
package control.servlet;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import bean.CharacterBEAN;
import dao.CharacterDAO;

/**
 * Servlet implementation class Bank
 */
public class Bank extends HttpServlet {
	private static final long	serialVersionUID	= 1L;
	private CharacterDAO		characterDAO;

	public void init( ServletConfig config ) throws ServletException {
		characterDAO = (CharacterDAO) config.getServletContext().getAttribute(
				"CharacterDAO" );
	}

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Bank() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet( HttpServletRequest request,
			HttpServletResponse response ) throws ServletException, IOException {
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost( HttpServletRequest request,
			HttpServletResponse response ) throws ServletException, IOException {
		String action = request.getParameter( "action" );
		HttpSession session = request.getSession();
		if( session.getAttribute( "ID" ) != null ) {
			int accountID = (Integer) session.getAttribute( "ID" );
			CharacterBEAN characterBEAN = characterDAO
					.getCharacterByID( accountID );
			request.setAttribute( "character", characterBEAN );
			if( action.equals( "postMoney" ) ) {
				int amount = Integer.parseInt( request.getParameter( "money" ) );
				characterDAO.setCashMoney(
						( characterBEAN.getMoneyCash() - amount ), accountID );
				characterDAO.setBankMoney(
						( characterBEAN.getMoneyBank() + amount ), accountID );
				CharacterBEAN c = characterDAO.getCharacterByID( accountID );
				request.setAttribute( "character", c ); // overwrite character
														// with the correct data
				goTo( "WEB-INF/logged/bank.jsp", request, response );
				return;
			} else if( action.equals( "withdrawMoney" ) ) {
				int amount = Integer.parseInt( request.getParameter( "money" ) );
				characterDAO.setBankMoney(
						( characterBEAN.getMoneyBank() - amount ), accountID );
				characterDAO.setCashMoney(
						( characterBEAN.getMoneyCash() + amount ), accountID );
				CharacterBEAN c = characterDAO.getCharacterByID( accountID );
				request.setAttribute( "character", c ); // overwrite
														// character with
														// the correct data
				goTo( "WEB-INF/logged/bank.jsp", request, response );
				return;
			} else {
			}
		} else {
		}
	}

	private void goTo( String action, HttpServletRequest request,
			HttpServletResponse response ) throws ServletException, IOException {
		try {
			RequestDispatcher dispatcher = request
					.getRequestDispatcher( action );
			dispatcher.forward( request, response );
		} catch( Exception e ) {
			e.printStackTrace();
		}
	}
}
